	
********************************************************************************
*** Time shifts for synthetic controls
*** Forward shift only (lags only)
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/forwardonly

*********************************************************************
*** Fit treated on control lags to find best fit  
**** Need to do this for each of the treated

local StJoTreat = mofd(mdy(11,16,2004))
*local StJoSettle = mofd(mdy(12,21,2007))

local WheatonTreat = mofd(mdy(10,19,2004))
*local WheatonSettle = mofd(mdy(1,7,2010))

local ElCentroTreat = mofd(mdy(3,20,2006))
*local ElCentroSettle = mofd(mdy(9,20,2010))

local OverlookTreat = mofd(mdy(4,25,2008))
*local OverlookSettle = mofd(mdy(6,21,2012))

local MortonPlantTreat = mofd(mdy(8,27,2008)) 
*local MortonPlantSettle = mofd(mdy(11,20,2012))

local ShandsTreat = mofd(mdy(8,30,2008))
*local ShandsSettle = mofd(mdy(8,8,2013))

local chsTreat = mofd(mdy(1,7,2009))
*local chsSettle= mofd(mdy(8,4,2014))






local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
foreach dft of local dftlist {
	clear all 
	display "`dft'"

	** Controls handled separately as random sample


	** Keep only sample and defendant 
	use op_ipop_controls_panel.dta, clear
	keep if prvdrnum == "`dft'"
	drop control
	append using "controls/`dft'_controls.dta"

	*** Encode controls and treated variable
	encode(prvdrnum), gen(group)
	tsset group dt
	count if group == 999
	assert(r(N) == 0)
	replace group = 999 if prvdrnum == "`dft'"

	*** Save prvdrnum group crosswalk
	preserve
	keep prvdrnum group
	duplicates drop
	save "controls/`dft'_controlgroup_crosswalk.dta", replace
	restore


	*** Prep for looping lags (no leads)
	levelsof group, local(levels)
	keep group dt pmt_amt 
	reshape wide pmt_amt, i(dt) j(group)
	tsset dt
	mata: distanceLag = J(1,3,0)
	* 3 columns: group, lag period, distance

	set matsize 11000


	** Only want to keep treated pre-period 
	local treatment_period = ``dft'Treat'
	replace pmt_amt999 = . if dt >= `treatment_period'

	** Find max lag so that there are 3 years of pre-period data 
	summarize dt 
	local maxlag = max(`treatment_period' - r(min) - 36, 0)
		** Treatment period - min  = length of end window 
	

	*** Generate series of 0s for use in computing diff
	gen diff = 0 

	*** Loop, storing lags and fit
	foreach g of local levels {
		disp "Looping for defendant `dft', control `g'"
		
		* Skip treated group
		if `g' == 999{
				continue
			}

		disp "Maximum Lag: `maxlag'"

		foreach t of numlist 0/`maxlag'{
			*disp `t'
			qui replace diff = (pmt_amt999 - L`t'.pmt_amt`g')^2
			qui summarize diff 
			local dist = r(sum)/r(N)
			mata: distanceLag=  distanceLag \ `g', `t', `dist'
			}
	}

	mata: st_matrix("distanceLag", distanceLag)


	svmat distanceLag
	keep distanceLag*

	rename distanceLag1 laggroup
	rename distanceLag2 lagperiod
	rename distanceLag3 lagdistance

	drop if laggroup == 0
	cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/forwardonly
	save "distance/distancelag_`dft'.dta", replace
}

	********************************************************************************
	*** Find best fit per group

local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
foreach dft of local dftlist {
	cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/forwardonly
	
	use "distance/distancelag_`dft'.dta", clear

	* Create best fit per group
	keep laggroup lagperiod lagdistance
	drop if mi(laggroup)
	bysort laggroup: egen minlagdistance = min(lagdistance)
	keep if lagdistance == minlagdistance
	* Get rid of dupes
	collapse (min) lagperiod (min) lagdistance, by(laggroup)

	* Prep for merge
	rename laggroup group
	rename lagdistance mindistance 
	replace lagperiod = -1*lagperiod
	rename lagperiod period
	save "distance/groupleadlags_`dft'.dta", replace

}

